package com.gis.bigdata.spark.core.rdd.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import java.text.SimpleDateFormat
import java.util.Date

/**
 * @author LnnuUser
 * @create 2021-08-27-下午6:36
 */
object Spark06_RDD_Operator_Transform_Test {

  def main(args: Array[String]): Unit = {

    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc: SparkContext = new SparkContext(sparkConf)


    // TODO 算子 --mapPartitions
    val rdd: RDD[String] = sc.textFile("datas/apache.log")

    val timeRDD: RDD[(String, Iterable[(String, Int)])] = rdd.map(
      line => {
        val datas: Array[String] = line.split(" ")
        val time = datas(3)
        // time.substring(0,)
        // 17/05/2015:10:05:57
        val sdf: SimpleDateFormat = new SimpleDateFormat("dd/MM/yyyy:HH:mm:ss")
        // parse() ：传入的参数是时间，拿着时间格式去反解时间
        val date: Date = sdf.parse(time)
        val sdf1: SimpleDateFormat = new SimpleDateFormat("HH")
        // format() : 和parse刚好一反，去格式化时间
        val hour: String = sdf1.format(date)
        (hour, 1)
      }
    ).groupBy(_._1)

    timeRDD.map{
      case (hour, iter) =>
        (hour, iter.size)
    }.collect.foreach(println)

    sc.stop()

  }

}
